﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web.UI;
using TA.Turbo;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all entity list controls.
	/// </summary>
	/// <typeparam name="T">The entity type.</typeparam>
	/// <typeparam name="C">The type of list control.</typeparam>
	public abstract class BaseEntitySingleValueListControl<T, C> : BaseEntityControl<T, C>
		where C : BaseValueControl<string>, ISingleValueListControl, new()
	{
		#region PUBLIC
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseEntitySingleValueListControl()
		{

		}
		#region PROPS
		/// <summary>
		/// Returns a comma (',') separated list of text values of all selected items of this list control, or empty string if no items are selected. Use the same format (',' - separated list) to set selected items by their text values.
		/// </summary>
		public override string Text
		{
			get
			{
				if (this.Child.SelectedItem == null)
					return null;
				return this.Child.SelectedItem.Text;
			}
			set
			{
				if (value == null)
					Child.Value = null;
				else
				{
					this.Child.SelectedItem = this.Child.Items.FindByText(value);
				}
			}
		}
		/// <summary>
		/// Default text - used to set the list control's FirstItemText.
		/// </summary>
		public override string DefaultText
		{
			get
			{
				return base.DefaultText;
			}
			set
			{
				base.DefaultText = value;
				//if (Child != null)
				//  Child.FirstItemText = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		#endregion
		#region METHODS
		/// <summary>
		/// Assigns DefaultText to Child.FirstItemText.
		/// </summary>
		protected override void CreateChild()
		{
			base.CreateChild();

			this.AddFirstItems();
		}
		/// <summary>
		/// Adds necessary "first items" to the child list control. 
		/// </summary>
		/// <remarks>
		/// "First items" are displayed at the top of the list and are added in addition to any data-bound items.
		/// They serve the purpose of showing items like "Select a product type", "All product types", etc.
		/// </remarks>
		protected virtual void AddFirstItems()
		{
			if (!string.IsNullOrEmpty(this.DefaultText))
			{
				this.Child.FirstItems.Add(new ListItem
				{
					Value = "",
					Text = this.DefaultText
				});
			}
		}
		#endregion
		#endregion
	}
}
